const fs = require('fs');
const path = require('path');


const http = require('http');
let server = http.createServer()
server.listen(3000, () => {
    console.log("3000监听成功");
})


// 根据用户请求的不同路径，响应不同的内容
server.on("request", (req, res) => {


    if (req.url == "/favicon.ico") {
        res.end()
        return
    }


    let { pathname } = new URL(req.url, "http://localhost:3000/")


    if (pathname == "/index") {

        let filepath = path.join(__dirname, "../html/index.html")
        let result = fs.readFileSync(filepath)
        res.end(result)

    } else {

        // 处理静态资源
        // console.log(".."+pathname);
        // res.end("Ok")

        let filepath = path.join(__dirname, ".." + pathname)
        let result = fs.readFileSync(filepath)
        res.end(result)

    }

})
